
package com.huizhan.web.controller;
 
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.domain.Sort.*;
import io.swagger.annotations.*;
import java.util.*;

import com.huizhan.web.view.*;
import com.huizhan.web.dao.*;
import com.huizhan.web.model.*;
/**
 * 项目停车api
 */
 
@Api(tags = "ProjectCar",description = "项目停车")
@RestController
public class ProjectCarController {
    
    @Autowired
    private ProjectCarDao dao;
   
    /**
     * 获取项目停车集合
     * @return api结果
     */
    @ApiOperation(value = "获取项目停车集合")
    @RequestMapping(value =  "/api/ProjectCar/",method = {RequestMethod.GET})
    public ApiResult<List<ProjectCar>> Get(){
         ApiResult<List<ProjectCar>> result=new ApiResult<List<ProjectCar>>();
        try 
        {
            List<ProjectCar> res = dao.findAll();
            result.setData(res);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 根据ID获取项目停车信息
     * @param id 项目停车ID
     * @return api结果
     */
    @ApiOperation(value = "根据ID获取项目停车信息")
    @RequestMapping(value =  "/api/ProjectCar/{id}",method = {RequestMethod.GET})
    public ApiResult<ProjectCar> Get(@PathVariable Long id){
        ApiResult<ProjectCar> result=new ApiResult<ProjectCar>();
        try
        {
            ProjectCar model= dao.getOne(id);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 修改项目停车信息
     * @param id 项目停车ID
     * @param model 项目停车信息
     * @return api结果
     */
    @ApiOperation(value = "修改项目停车信息")
    @RequestMapping(value =  "/api/ProjectCar/{id}",method = {RequestMethod.PUT})
    public ApiResult<ProjectCar> Edit(@PathVariable Long id,@RequestBody ProjectCar model){
        ApiResult<ProjectCar> result=new ApiResult<ProjectCar>();
        try
        {
            model.setProjectCarId(id);
            dao.save(model);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 添加项目停车信息
     * @param model 项目停车信息
     * @return api结果
     */
    @ApiOperation(value = "添加项目停车信息")
    @RequestMapping(value =  "/api/ProjectCar/",method = {RequestMethod.POST})
    public ApiResult<ProjectCar> Add(@RequestBody ProjectCar model){
         ApiResult<ProjectCar> result=new ApiResult<ProjectCar>();
        try
        {
            model.setCreateDate(new Date());
            dao.save(model);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
    
    /**
     * 删除项目停车信息
     * @param id 项目停车ID
     * @return api结果
     */
    @ApiOperation(value = "删除项目停车信息")
    @RequestMapping(value =  "/api/ProjectCar/{id}",method = {RequestMethod.DELETE})
    public ApiResult<Object> Delete(@PathVariable  Long id){
        ApiResult<Object> result=new ApiResult<Object>();
        try 
        {
            dao.deleteById(id);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
    
    /**
     * 查询项目停车集合
     * @param model 账号信息
     * @return api结果
     */
    @ApiOperation(value = "查询项目停车集合")
    @RequestMapping(value =  "/api/ProjectCar/Search",method = {RequestMethod.POST})
    public ApiResult<List<ProjectCar>> Search(@RequestBody PageCar model){
         ApiResult<List<ProjectCar>> result=new ApiResult<List<ProjectCar>>();
         
        try
        {    
            PageRequest pageable = PageRequest.of(model.getPageIndex(),model.getPageSize(),Direction.DESC,"Project_Car_Id") ;            
            Page<ProjectCar> page = dao.findByWhere(model.getProjectId(),model.getName(),model.getPhone(),pageable);
            result.setData(page.getContent());
            result.setTotal(page.getTotalElements());
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
}

